package org.brisling.workflow.cmd;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;

/**
 * 
 * <p>Title: JumpCmd</p>
 * <p>Description: 流程节点跳转</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年4月14日 上午3:24:21
 */
public class JumpCmd  implements Command<ExecutionEntity>{
	private String processInstanceId;
    private String activityId;
    private ActivityImpl activityImpl;
    public static final String REASION_DELETE = "deleted";

    public JumpCmd(String processInstanceId, String activityId) {
        this.processInstanceId = processInstanceId;
        this.activityId = activityId;
    }
    
    public JumpCmd(String processInstanceId, ActivityImpl activityImpl) {
        this.processInstanceId = processInstanceId;
        this.activityImpl = activityImpl;
    }

    @Override
    public ExecutionEntity execute(CommandContext commandContext) {
        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);

        executionEntity.destroyScope(REASION_DELETE);
        if(activityImpl==null){
	        ProcessDefinitionImpl processDefinition = executionEntity.getProcessDefinition();
	        activityImpl = processDefinition.findActivity(activityId);
        }
        executionEntity.executeActivity(activityImpl);

        return executionEntity;
    }
}
